﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessObject;
using BusinessLogic;
using Utility;
using System.Web.Security;
using System.IO;
namespace WebTeamplate
{

    public partial class Admin_EditProfile : BasePage
    {
        #region Fields

        #endregion

        #region ViewStates
       
        #endregion

        #region Init,Loads
        protected void Page_Load(object sender, EventArgs e)
        {
            ClientDefaultButton.SetPageDefaultButton(this.Page, btnSave.ClientID);
            if (!Page.IsPostBack)
            {
               CommonFunction.LoadSubCate(drlGender,CommonFunction.DEFAULT_GENDER_CODE,true);
                InitTextControl();
                LoadUserInfor();
                txtName.Focus();
            }

        }
        #endregion

        #region Methos

        private void LoadUserInfor()
        {
            UserProfile objProfile = UserProfileUtil.Get(this.applicationName, this.userloginId);
            if (objProfile != null)
            {
                MembershipUser objUser = Membership.GetUser(objProfile.UserId);
                if (objUser != null)
                {
                    txtAddress.Text = objProfile.Address;
                    txtBirthDay.Text = ConvertValueObject.ParseString(objProfile.BirthDay);
                    txtEmail.Text = objUser.Email;
                    txtName.Text = objProfile.Name;
                    txtUserName.Text = objUser.UserName;
                    drlGender.SelectedValue = objProfile.Gender.ToString();
                }
            }
        }
        private void InitTextControl()
        {
           // imgBirthDay.Attributes.Add("onclick", "displayCalendar('../calendar/images/',document.forms[0]." + txtBirthDay.ClientID + ",'" + BaseFunctions.DEFAULT_DATE_FORMAT + "',this)");
            string validateGroup = "user";
            btnSave.Text = Resources.Resource.LABEL_BUTTON_SAVE;
            refvName.ErrorMessage = string.Format(Resources.Resource.MESSAGE_REQUIRE_INPUT, CommonFunction.GetTextLabel(lblName));
            refvName.ValidationGroup = validateGroup;
            refvUserName.ErrorMessage = string.Format(Resources.Resource.MESSAGE_REQUIRE_INPUT, CommonFunction.GetTextLabel(lblUserName));
            refvUserName.ValidationGroup = validateGroup;
          //  refvEmail.ErrorMessage = string.Format(Resources.Resource.MESSAGE_REQUIRE_INPUT, CommonFunction.GetTextLabel(lblEmail));
           // refvEmail.ValidationGroup = validateGroup;
            regvEmail.ErrorMessage = string.Format(Resources.Resource.MESSAGE_VALIDATE_INPUT, CommonFunction.GetTextLabel(lblEmail));
            regvEmail.ValidationGroup = validateGroup;
          //  refvBirthDay.ErrorMessage = string.Format(Resources.Resource.MESSAGE_REQUIRE_INPUT, CommonFunction.GetTextLabel(lblBirthDay));
          //  refvBirthDay.ValidationGroup = validateGroup;
            cusvBirthDay.ErrorMessage = string.Format(Resources.Resource.MESSAGE_VALIDATE_INPUT, CommonFunction.GetTextLabel(lblBirthDay));
            cusvBirthDay.ValidationGroup = validateGroup;
            refvGender.ErrorMessage = string.Format(Resources.Resource.MESSAGE_REQUIRE_INPUT, CommonFunction.GetTextLabel(lblGender));
            refvGender.ValidationGroup = validateGroup;
           // refvAddress.ErrorMessage = string.Format(Resources.Resource.MESSAGE_REQUIRE_INPUT, CommonFunction.GetTextLabel(lblAddress));
          //  refvAddress.ValidationGroup = validateGroup;
            vsumCategory.ValidationGroup = validateGroup;
            btnSave.ValidationGroup = validateGroup;

        }
        private void UpdateUser()
        {
              try
              {
                  MembershipUser user = Membership.GetUser(txtUserName.Text);
                  //check email is exist
                  string userNameCheck = Membership.GetUserNameByEmail(txtEmail.Text);
                  if (userNameCheck != null && !userNameCheck.Equals(txtUserName.Text))
                  {
                      lblErrorMessage.Text = string.Format(Resources.Resource.MESSAGE_EXISTS_ITEM, CommonFunction.GetTextLabel(lblEmail));//Email is exist
                      return;
                  }
                  user.Email = txtEmail.Text;
                  Membership.UpdateUser(user);                  
                  UserProfile objUser = new UserProfile();
                  objUser.Address = txtAddress.Text;
                  objUser.BirthDay = ConvertValueObject.ParseDateTime(txtBirthDay.Text);
                  objUser.LastUpdatedBy = this.userLoginName;
                  objUser.LastUpdatedDate = CommonFunction.Now;
                  objUser.Gender = ConvertValueObject.ParseInt(drlGender.SelectedValue.ToString());
                  objUser.Name = txtName.Text;
                  objUser.Phone = string.Empty;
                  objUser.UserId = new Guid(user.ProviderUserKey.ToString());
                  //
                  if (!UserProfileUtil.Update(objUser))
                       lblErrorMessage.Text = Resources.Resource.MESSAGE_UPDATE_ERROR;                      
                  else
                      lblErrorMessage.Text = Resources.Resource.MESSAGE_UPDATE_SUCCESSFULL;                    

              }
              catch (MembershipCreateUserException e)
              {
                  lblErrorMessage.Text = Resources.Resource.MESSAGE_UPDATE_ERROR;       
              }
        }

        
        #endregion

        #region Events, Handler

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
                UpdateUser();
            else
            {
                vsumCategory.ShowMessageBox = false;
                vsumCategory.ShowSummary = true;
            }
        }
        #endregion
    }
}